﻿using MediatR;
using SheepCultivation.API.AddliCation.BreedingD;
using SheepCultivation.Common.Domain.main;
using SheepCultivation.Common.Domain.Sheep;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.Parturitions
{
    public class ParturitionSeachHandler : IRequestHandler<ParturitionSeachCommon, Result>
    {
        #region 仓储
        /// <summary>
        /// 分娩仓储
        /// </summary>
        public readonly IBaseRepository<Parturition> ParturitionRepository;
        /// <summary>
        /// 栋舍仓储
        /// </summary>
        public readonly IBaseRepository<Building> BuildingRepository;
        /// <summary>
        /// 栋舍类型仓储
        /// </summary> 
        public readonly IBaseRepository<BuildingType> BuildingTypeRepository;
        /// <summary>
        /// 栏位仓储
        /// </summary>
        public readonly IBaseRepository<Columns> ColumnsRepository;
        /// <summary>
        /// 首次妊检提示仓储
        /// </summary>
        public readonly IBaseRepository<Pregnancy> PregnancyRepository;
        /// <summary>
        /// 耳号仓储
        /// </summary>
        public readonly IBaseRepository<BasicInformation> BasicInformationRepository;
        /// <summary>
        /// 种母分娩仓储
        /// </summary>
        public readonly IBaseRepository<SheepChildbirth> SheepChildbirthRepository;
        /// <summary>
        /// 日志
        /// </summary>
        public readonly ILogger<AppearancesSeachHandler> logger;
        /// <summary>
        /// 仓储
        /// </summary>
        /// <param name="parturitionRepository"></param>
        /// <param name="BuildingRepository"></param>
        /// <param name="BuildingTypeRepository"></param>
        /// <param name="ColumnsRepository"></param>
        /// <param name="PregnancyRepository"></param>
        /// <param name="BasicInformationRepository"></param>
        /// <param name="SheepChildbirthRepository"></param>
        /// <param name="logger"></param>
        public ParturitionSeachHandler(IBaseRepository<Parturition> parturitionRepository, IBaseRepository<Building> BuildingRepository, IBaseRepository<BuildingType> BuildingTypeRepository, IBaseRepository<Columns> ColumnsRepository, IBaseRepository<Pregnancy> PregnancyRepository, IBaseRepository<BasicInformation> BasicInformationRepository, IBaseRepository<SheepChildbirth> SheepChildbirthRepository, ILogger<AppearancesSeachHandler> logger)
        {
            ParturitionRepository = parturitionRepository;
            this.PregnancyRepository = PregnancyRepository;
            this.BuildingRepository = BuildingRepository;
            this.BuildingTypeRepository = BuildingTypeRepository;
            this.ColumnsRepository = ColumnsRepository;
            this.BasicInformationRepository = BasicInformationRepository;
            this.SheepChildbirthRepository = SheepChildbirthRepository;
            this.logger = logger;
        }
        #endregion

        #region 分娩提示显示列表
        /// <summary>
        /// 分娩提示显示列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(ParturitionSeachCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.error };
            //异常处理
            try
            {
                logger.LogInformation("分娩提示显示列表成功!");
                var data = new pagesssDto() { };
                var list = (from a in SheepChildbirthRepository.GetAll()
                            join b in BuildingRepository.GetAll() on a.HouseName equals b.BuildingName
                            join c in ColumnsRepository.GetAll() on a.HouseName equals c.ColumnsName
                            join d in BasicInformationRepository.GetAll() on a.SheepChildbirthEarNumber equals d.Auricle
                            join e in ParturitionRepository.GetAll() on d.BreedingSheepArchiveId equals e.BreedingSheepArchiveId
                            select new ParturitionDto()
                            {
                                SheepChildbirthId=a.SheepChildbirthId,
                                SheepChildbirthPregnancyDay = a.SheepChildbirthPregnancyDay,
                                SheepChildbirthEarNumber=a.SheepChildbirthEarNumber,
                                HouseName=a.HouseName,
                                HouseNumber=a.HouseNumber,                
                                BuildingId=b.BuildingId,
                                BuildingName=b.BuildingName,
                                ColumnsId=c.ColumnsId,
                                ColumnsName =c.ColumnsName,
                                BreedingSheepArchiveId=d.BreedingSheepArchiveId,
                                Auricle=d.Auricle,
                                ParturitionId = e.ParturitionId,
                                ParturitionNode = e.ParturitionNode,
                            }).ToList();
                //var list = (from a in ParturitionRepository.GetAll()
                //            join b in PregnancyRepository.GetAll() on a.BreedingSheepArchiveId equals b.BreedingSheepArchiveId
                //            join c in ColumnsRepository.GetAll() on a.ColumnsId equals c.ColumnsId
                //            join d in BuildingRepository.GetAll() on a.BuildingId equals d.BuildingId
                //            join e in BasicInformationRepository.GetAll() on b.BreedingSheepArchiveId equals e.BreedingSheepArchiveId
                //            select new ParturitionDto()
                //            {
                //                ParturitionId = a.ParturitionId,
                //                ParturitionNode = a.ParturitionNode,
                //                ParturitionDate = a.ParturitionDate,
                //                ParturitionDelivery = a.ParturitionDelivery,
                //                BreedingSheepArchiveId = b.BreedingSheepArchiveId,
                //                PregnancyName = b.PregnancyName,
                //                PregnancyDay = b.PregnancyDay,
                //                ColumnsId = c.ColumnsId,
                //                ColumnsName = c.ColumnsName,
                //                BuildingId = d.BuildingId,
                //                BuildingName = d.BuildingName,
                //                Auricle = e.Auricle
                //            }).ToList();

                //分页
                data.totalCount = list.Count();
                data.pageCount = (int)((list.Count() * 1.0) / request.pageSize);
                data.data = list.OrderBy(x => x.ParturitionId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();

                result.Code = (int)CommonEnumCode.success;
                result.Data = data;
                result.Msg = "分娩提示显示列表成功";
            }
            catch (Exception ex)
            {
                logger.LogError("分娩提示显示列表失败!" + ex.Message);
                result.Msg = "分娩提示显示列表失败";
            }
            return result;
        }
        #endregion
    }

    /// <summary>
    /// 分页
    /// </summary>
    public class pagesssDto
    {
        public int totalCount { get; set; }
        public int pageCount { get; set; }
        public List<ParturitionDto> data { get; set; }
    }

    /// <summary>
    /// 分娩提示Dto
    /// </summary>
    public class ParturitionDto()
    {
        /// <summary>
        /// 分娩提示Id
        /// </summary>
        public int ParturitionId { get; set; }

        /// <summary>
        /// 种母耳号
        /// </summary>
        public string? ParturitionNode { get; set; }
        /// <summary>
        /// 栏位Id
        /// </summary>
        public int ColumnsId { get; set; }
        /// <summary>
        /// 栋舍Id
        /// </summary>
        public int BuildingId { get; set; }
        /// <summary>
        /// 栏位名称
        /// </summary>
        public string? ColumnsName { get; set; }
        /// <summary>
        /// 栋舍名称
        /// </summary>
        public string? BuildingName { get; set; }
        /// <summary>
        /// 配种日期
        /// </summary>
        public DateTime ParturitionDate { get; set; }
        /// <summary>
        /// 预产日期
        /// </summary>
        public DateTime ParturitionDelivery { get; set; }
        public int SheepChildbirthId { get; set; }
        /// <summary>
        /// 妊娠天数
        /// </summary>
        public int SheepChildbirthPregnancyDay { get; set; }
        /// <summary>
        /// 耳号
        /// </summary>
        public string? SheepChildbirthEarNumber { get; set; }
        /// <summary>
        /// 当前栋舍
        /// </summary>
        public string? HouseName { get; set; }
        /// <summary>
        /// 当前栏位
        /// </summary>
        public int HouseNumber { get; set; }
        public int BreedingSheepArchiveId { get; set; }
        /// <summary> 
        /// 耳号
        /// </summary>
        public string Auricle { get; set; }
    };
}
